<template>
  <el-drawer :visible.sync="visible" :title="title" :modal-append-to-body="true" :append-to-body="true"
    :destroy-on-close="false" :modal="true" direction="rtl" :wrapperClosable="false" custom-class="form-drawer"
    size="450px" @closed="closed">
    <div class="form-drawer__content">
      <el-form :inline="true" class="demo-form-inline" style="flex:none;padding-top: 0px;
    padding-bottom: 5px;
    padding-left: 5px;">
        <el-form-item style="margin-bottom:0px;">
          <el-input v-model="queryForm.keywords" prefix-icon="el-icon-search" placeholder="关键字" style="width: 14.5rem"
            @input="onQuerySubmit" />
        </el-form-item>
      </el-form>
      <el-table ref="tableDataID" :data="tableData" border empty-text="暂无数据." highlight-current-row style="width: 100%">
        <el-table-column type="selection" width="40">
        </el-table-column>
        <el-table-column type="index" label="序号" width="50" align="center" :index="indexMethod">
        </el-table-column>
        <el-table-column prop="type" label="类型" width="60">
          <template slot-scope="scope">
            <span v-if="scope.row.type=='POI' ">POI点</span>
            <span v-if="scope.row.type=='PATH' ">路径</span>
            <span v-if="scope.row.type=='RANGE' ">区域</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="图层名称" />
      </el-table>
      <div class="form-drawer__footer">
        <el-button @click="closed" :loading="loading">取 消</el-button>
        <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      </div>
    </div>

  </el-drawer>
</template>

<script>
  import {
    findLayerAll,
  } from '@/api/subsystem/layer'
  import {
    saveMenuLayers,
  } from '@/api/subsystem/scenemenu'
  import {
    isOk
  } from '@/utils/request'
  export default {
    name: 'LayerDialog',
    components: {},
    props: {
      properties: {
        type: Object,
        default: () => ({})
      }
    },
    computed: {},
    watch: {
      properties: {
        handler: function(value) {
          this.visible = value.visible
          this.menuId = value.menuId
        },
        immediate: true,
        deep: false
      }
    },
    data() {
      return {
        visible: false,
        title: '场景图层',
        menuId: '',
        loading: false,
        tableData: [],
        queryForm: {
          keywords: undefined,
        },
      }
    },
    created() {
      this.load()
    },
    methods: {
      indexMethod(index) {
        return index + 1;
      },
      onQuerySubmit(event) {
        this.load()
      },
      load() {
        this.loading = true
        findLayerAll({
            projectId: this.$store.getters.projectId,
            menuId: this.menuId,
            keywords: this.queryForm.keywords
          }).then(rsp => {
            if (isOk(rsp)) {
              this.tableData = rsp.result
              this.loading = false
            }
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
      },
      submitForm: function() {
        let select = this.$refs.tableDataID.selection
        if (select.length == 0) {
          this.$message.warning('请选择图层！');
          return;
        }
        let layerIds = ''
        select.forEach(item => {
          layerIds += item.id + ','
        })
        saveMenuLayers({
            projectId: this.$store.getters.projectId,
            menuId: this.menuId,
            layerIds: layerIds.substring(0, layerIds.length - 1)
          }).then(rsp => {
            if (200 == rsp.code) {
              this.$message({
                type: 'success',
                message: rsp.message
              })
            }
            this.closed()
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
      },
      closed: function() {
        this.$emit('closed', false)
      }
    }
  }
</script>


<style scoped>
  /deep/.form-drawer__footer button {
    flex: 1;
  }

  /deep/.el-drawer__body {
    overflow-y: auto;
  }

  /deep/.form-drawer__content {
    overflow-y: auto;
  }

  /deep/.el-table {
    overflow-y: auto;
  }
</style>
